import apiClient from './api'

// 学习数据接口定义
export interface LearningData {
  id: string
  title: string
  type: 'text' | 'document' | 'image' | 'video'
  content: string
  source?: string
  uploadTime: string
  lastModified: string
  status: 'pending' | 'reviewed' | 'approved' | 'rejected'
  relevance: number
  tags: string[]
  metadata?: Record<string, any>
}

// 训练任务接口定义
export interface TrainingTask {
  id: string
  name: string
  description?: string
  model: string
  dataset: string[]
  hyperparameters: Record<string, any>
  status: 'pending' | 'running' | 'completed' | 'failed'
  progress: number
  startTime?: string
  endTime?: string
  metrics?: Record<string, any>
  createdBy: string
}

// LoRA适配器接口定义
export interface LoRAAdapter {
  id: string
  name: string
  description?: string
  baseModel: string
  adapterPath: string
  size: number
  version: string
  status: 'inactive' | 'active' | 'loading'
  createdAt: string
  metrics?: Record<string, any>
  isDefault?: boolean
}

// 学习服务类
class LearningService {
  // 上传学习数据
  async uploadLearningData(data: Omit<LearningData, 'id' | 'uploadTime' | 'lastModified' | 'status'>) {
    const response = await apiClient.post('/v1/learning/data', data)
    return response
  }

  // 获取学习数据列表
  async getLearningDataList(params?: {
    page?: number
    pageSize?: number
    search?: string
    type?: string
    status?: string
    tags?: string[]
    sortBy?: string
    sortOrder?: 'asc' | 'desc'
  }) {
    const response = await apiClient.get('/v1/learning/data', { params })
    return response
  }

  // 获取学习数据详情
  async getLearningDataDetail(dataId: string) {
    const response = await apiClient.get(`/v1/learning/data/${dataId}`)
    return response
  }

  // 更新学习数据
  async updateLearningData(dataId: string, data: Partial<LearningData>) {
    const response = await apiClient.put(`/v1/learning/data/${dataId}`, data)
    return response
  }

  // 删除学习数据
  async deleteLearningData(dataId: string) {
    const response = await apiClient.delete(`/v1/learning/data/${dataId}`)
    return response
  }

  // 批量删除学习数据
  async batchDeleteLearningData(dataIds: string[]) {
    const response = await apiClient.delete('/v1/learning/data/batch', {
      data: { dataIds }
    })
    return response
  }

  // 创建训练任务
  async createTrainingTask(task: Omit<TrainingTask, 'id' | 'status' | 'progress' | 'startTime' | 'endTime'>) {
    const response = await apiClient.post('/v1/learning/tasks', task)
    return response
  }

  // 获取训练任务列表
  async getTrainingTasks(params?: {
    page?: number
    pageSize?: number
    search?: string
    model?: string
    status?: string
    sortBy?: string
    sortOrder?: 'asc' | 'desc'
  }) {
    const response = await apiClient.get('/v1/learning/tasks', { params })
    return response
  }

  // 获取训练任务详情
  async getTrainingTaskDetail(taskId: string) {
    const response = await apiClient.get(`/v1/learning/tasks/${taskId}`)
    return response
  }

  // 更新训练任务
  async updateTrainingTask(taskId: string, data: Partial<TrainingTask>) {
    const response = await apiClient.put(`/v1/learning/tasks/${taskId}`, data)
    return response
  }

  // 启动训练任务
  async startTrainingTask(taskId: string) {
    const response = await apiClient.post(`/v1/learning/tasks/${taskId}/start`)
    return response
  }

  // 停止训练任务
  async stopTrainingTask(taskId: string) {
    const response = await apiClient.post(`/v1/learning/tasks/${taskId}/stop`)
    return response
  }

  // 删除训练任务
  async deleteTrainingTask(taskId: string) {
    const response = await apiClient.delete(`/v1/learning/tasks/${taskId}`)
    return response
  }

  // 获取LoRA适配器列表
  async getLoRAAdapters(params?: {
    page?: number
    pageSize?: number
    search?: string
    baseModel?: string
    status?: string
  }) {
    const response = await apiClient.get('/v1/learning/lora-adapters', { params })
    return response
  }

  // 加载LoRA适配器
  async loadLoRAAdapter(adapterId: string) {
    const response = await apiClient.post(`/v1/learning/lora-adapters/${adapterId}/load`)
    return response
  }

  // 卸载LoRA适配器
  async unloadLoRAAdapter(adapterId: string) {
    const response = await apiClient.post(`/v1/learning/lora-adapters/${adapterId}/unload`)
    return response
  }

  // 删除LoRA适配器
  async deleteLoRAAdapter(adapterId: string) {
    const response = await apiClient.delete(`/v1/learning/lora-adapters/${adapterId}`)
    return response
  }

  // 提交学习反馈
  async submitLearningFeedback(dataId: string, feedback: {
    relevance?: number
    quality?: number
    comments?: string
  }) {
    const response = await apiClient.post(`/v1/learning/data/${dataId}/feedback`, feedback)
    return response
  }
}

export default new LearningService()